Fix fire-and-forget background task in LocalFeatureFlagsLoader - #161
Merged
Conversation
Contributor
posthog-dotnet Compliance ReportDate: 2026-02-26 02:42:45 UTC ✅ All Tests Passed!29/29 tests passed Capture Tests✅ 29/29 tests passed View Details
|
Add three tests for LocalFeatureFlagsLoader disposal: - DoesNotDisposeTwice: concurrent double-dispose completes without exception - CompletesGracefullyWhenPollingNeverStarted: dispose before any polling - Await disposeTask after timeout check to surface disposal exceptions Mark _pollingTask volatile for consistency with other cross-thread fields and to make the intent explicit, since unlike AsyncBatchHandler's readonly task fields, this one is lazily assigned after construction.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a shutdown race in local feature-flag polling by tracking and awaiting the background polling task during disposal, and by ensuring PostHogClient.DisposeAsync() keeps the API client alive until background work winds down.
Changes:
- Track the local feature-flag polling
Taskand await it duringLocalFeatureFlagsLoader.DisposeAsync(). - Reorder
PostHogClient.DisposeAsync()to dispose background components before disposing the API client. - Add a regression test to verify disposal completes while a poll is in-flight.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/PostHog/Features/LocalFeatureFlagsLoader.cs |
Stores the polling task and implements async disposal to cancel/await polling before disposing timer/CTS. |
src/PostHog/PostHogClient.cs |
Disposes background components before disposing _apiClient, avoiding shutdown-time ObjectDisposedException. |
tests/UnitTests/Features/LocalFeatureFlagsLoaderTests.cs |
Adds a test that blocks an in-flight poll and asserts DisposeAsync completes cleanly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Avoids wrapping exceptions in AggregateException, giving clearer stack traces from disposal paths. Applied consistently across all three sync-over-async Dispose bridges. Also fix misleading comment in LocalFeatureFlagsLoader — cancellation terminates the in-flight request, it doesn't wait for it to complete.
haacked
marked this pull request as ready for review
February 26, 2026 02:37
Ensures timers, CTS, and API clients are always disposed even if awaiting a background task throws. The exception still propagates — this just prevents resource leaks on top of bugs. Applied consistently to LocalFeatureFlagsLoader, AsyncBatchHandler, and PostHogClient.
marandaneto
approved these changes
Feb 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ObjectDisposedExceptionPostHogClient.DisposeAsync()so the API client remains alive while the polling task winds down_pollingTaskvolatile for cross-thread visibility consistencyGetAwaiter().GetResult()instead of.Wait()in all sync Dispose bridges for cleaner exception stack tracesFollows the same pattern established in
AsyncBatchHandler(PR #158).Fixes #159
Test plan
net8.0andnetcoreapp3.1CompletesGracefullyDuringInFlightPolltest passes on both frameworksDoesNotDisposeTwicetest passes on both frameworksCompletesGracefullyWhenPollingNeverStartedtest passes on both frameworks